package com.chengyu.core.service.shop.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.MemberRemindEnums;
import com.chengyu.core.domain.enums.ShopEnums;
import com.chengyu.core.domain.form.ShopSearchForm;
import com.chengyu.core.domain.result.ChatNotice;
import com.chengyu.core.domain.result.CustomerConstant;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.mapper.UmsShopAccountMapper;
import com.chengyu.core.mapper.UmsShopInfoMapper;
import com.chengyu.core.mapper.UmsShopMapper;
import com.chengyu.core.model.*;
import com.chengyu.core.service.im.ChatService;
import com.chengyu.core.service.member.MemberService;
import com.chengyu.core.service.shop.ShopAccountService;
import com.chengyu.core.service.shop.ShopConfigService;
import com.chengyu.core.service.shop.ShopInfoService;
import com.chengyu.core.service.system.VerifyCodeService;
import com.chengyu.core.service.walk.WalkMemberService;
import com.chengyu.core.utils.AmapUtil;
import com.chengyu.core.utils.CommonUtil;
import com.chengyu.core.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title  审核店铺
 * @author LeGreen
 * @date   2022/4/23
 */
@Service
public class ShopInfoServiceImpl implements ShopInfoService {


	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private VerifyCodeService verifyCodeService;

	@Autowired
	private UmsShopInfoMapper shopInfoMapper;
	@Autowired
	private UmsShopMapper shopMapper;
	@Autowired
	private ShopConfigService shopConfigService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private WalkMemberService walkMemberService;
	@Autowired
	private ChatService chatService;
	@Autowired
	private UmsShopAccountMapper shopAccountMapper;

	@Autowired
	private ShopAccountService shopAccountService;

	@Autowired
	private AmapUtil amapUtil;

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public Integer applyShopNew(UmsMember member, UmsShopInfo shopInfo) throws ServiceException {


		UmsShopInfoExample example = new UmsShopInfoExample();

		UmsShopInfoExample.Criteria criteria = example.createCriteria();

		criteria.andMemberIdEqualTo(member.getId());
		criteria.andStatusNotEqualTo(ShopEnums.ShopStatus.FAIL.getValue());
		if(shopInfo.getId() == null){
			//判断该用户是否申请过店铺
			long count = shopInfoMapper.countByExample(example);
			if(count > 0){
				throw new ServiceException("您已申请过店铺,或者正在申请中，请勿重复申请");
			}
		}
		//判断店铺是否已通过
//		UmsShopInfoExample example = new UmsShopInfoExample();
//		criteria.createCriteria();
//		criteria.andMemberIdEqualTo(member.getId());
//		criteria.andStatusEqualTo(ShopEnums.ShopStatus.SUCCESS.getValue());
//		long count = shopInfoMapper.countByExample(example);
//		if(count > 0){
//			throw new ServiceException("您已申请过店铺,请勿重复申请2");
//		}


		shopInfo.setStatus(ShopEnums.ShopStatus.SUBMIT_VERIFY.getValue());
		shopInfo.setUpdTime(DateUtil.date());
		if(shopInfo.getId() == null){
			shopInfo.setAddTime(shopInfo.getUpdTime());
			shopInfoMapper.insertSelective(shopInfo);
		}else{
			shopInfoMapper.updateByPrimaryKeySelective(shopInfo);
		}


//		//注册子账号
//		if(isShop) {
//			UmsShopAccount account = new UmsShopAccount();
//			account.setMemberId(member.getId());
//			account.setUsername(member.getCode());
//			account.setPassword(oriPassword);
//			account.setPhone(member.getPhone());
//			account.setMainInd(CommonConstant.YES_INT);
//			account.setRealname(member.getCode()+":主账号");
//			shopAccountService.register(account);
//		}




		ChatNotice notice = new ChatNotice();
		notice.setTitle("「"+shopInfo.getName()+"」申请新开店铺, 请尽快审批");
		notice.setContent(JSONUtil.toJsonStr(shopInfo));
		notice.setType(MemberRemindEnums.AdminRemindTypes.WAIT_VERIFY_SHOP.getType());
		chatService.sendNoticeMsg(CustomerConstant.ADMIN_MEMBER_ID, JSONUtil.toJsonStr(notice));



		return shopInfo.getId();
	}



	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public Integer applyShopCert(UmsMember member, UmsShopInfo shopInfo) throws ServiceException {
		if(shopInfo.getId() == null){
			//判断该用户是否申请过店铺
			UmsShopInfoExample example = new UmsShopInfoExample();
			example.createCriteria().andMemberIdEqualTo(member.getId()).andShoptypeEqualTo(shopInfo.getShoptype());
			long count = shopInfoMapper.countByExample(example);
			if(count > 0){
				throw new ServiceException("您已申请过,请勿重复申请");
			}
		}
		//判断店铺是否已通过
		UmsShopInfoExample example = new UmsShopInfoExample();
		example.createCriteria().andMemberIdEqualTo(member.getId()).andShoptypeEqualTo(shopInfo.getShoptype()).andStatusEqualTo(ShopEnums.ShopStatus.SUCCESS.getValue());
		long count = shopInfoMapper.countByExample(example);
		if(count > 0){
			throw new ServiceException("您已申请过,请勿重复申请");
		}
		shopInfo.setStatus(ShopEnums.ShopStatus.SUBMIT_CERT.getValue());
		shopInfo.setUpdTime(DateUtil.date());
		if(shopInfo.getId() == null){
			shopInfo.setAddTime(shopInfo.getUpdTime());
			shopInfoMapper.insertSelective(shopInfo);
		}else{
			shopInfoMapper.updateByPrimaryKeySelective(shopInfo);
		}
		return shopInfo.getId();
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void applyShopDetail(UmsShopInfo shopInfo) {
		shopInfo.setUpdTime(DateUtil.date());
		shopInfo.setStatus(ShopEnums.ShopStatus.SUBMIT_SHOP.getValue());
		shopInfoMapper.updateByPrimaryKeySelective(shopInfo);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void submitShopInfo(UmsShopInfo shopInfo) {
		shopInfo.setUpdTime(DateUtil.date());
		shopInfo.setStatus(ShopEnums.ShopStatus.SUBMIT_VERIFY.getValue());
		shopInfoMapper.updateByPrimaryKeySelective(shopInfo);

		ChatNotice notice = new ChatNotice();
		notice.setTitle("「"+shopInfo.getName()+"」申请新开店铺, 请尽快审批");
		notice.setContent(JSONUtil.toJsonStr(shopInfo));
		notice.setType(MemberRemindEnums.AdminRemindTypes.WAIT_VERIFY_SHOP.getType());
		chatService.sendNoticeMsg(CustomerConstant.ADMIN_MEMBER_ID, JSONUtil.toJsonStr(notice));
	}

	@Override
	public UmsShopInfo getShopInfo(UmsMember member) {
		UmsShopInfoExample example = new UmsShopInfoExample();
		example.createCriteria().andMemberIdEqualTo(member.getId());
		List<UmsShopInfo> list = shopInfoMapper.selectByExample(example);
		return CollectionUtil.isNotEmpty(list) ? list.get(0) : null;
	}

	@Override
	public List<UmsShopInfo> getShopInfoList(ShopSearchForm form, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		UmsShopInfoExample example = new UmsShopInfoExample();
		example.setOrderByClause("add_time desc");
		UmsShopInfoExample.Criteria criteria = example.createCriteria();

		if(form.getMemberId() != null){
			criteria.andMemberIdEqualTo(form.getMemberId());
		}
		if(StringUtils.isNotBlank(form.getMemberName())){
			criteria.andMemberNameEqualTo(form.getMemberName());
		}
		if(form.getType() != null){
			criteria.andTypeEqualTo(form.getType());
		}
		if(form.getShoptype() != null){
			criteria.andShoptypeEqualTo(form.getShoptype());
		}
		if(StringUtils.isNotBlank(form.getRealname())){
			criteria.andRealnameEqualTo(form.getRealname());
		}
		if(StringUtils.isNotBlank(form.getIdcard())){
			criteria.andIdcardEqualTo(form.getIdcard());
		}
		if(StringUtils.isNotBlank(form.getPhone())){
			criteria.andPhoneEqualTo(form.getPhone());
		}
		if(StringUtils.isNotBlank(form.getName())){
			criteria.andNameEqualTo(form.getName());
		}
		if(StringUtils.isNotBlank(form.getWeixin())){
			criteria.andWeixinEqualTo(form.getWeixin());
		}
		if(form.getStatus() != null){
			criteria.andStatusEqualTo(form.getStatus());
		}

		if(form.getNotStatus() != null){
			criteria.andStatusNotEqualTo(form.getNotStatus());
		}

		return shopInfoMapper.selectByExample(example);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void verifyShop(Integer shopInfoId, Integer status, String reason) throws ServiceException {
		UmsShopInfo shopInfo = shopInfoMapper.selectByPrimaryKey(shopInfoId);
		if(shopInfo == null || shopInfo.getStatus() != ShopEnums.ShopStatus.SUBMIT_VERIFY.getValue()){
			throw new ServiceException("当前店铺状态不正确");
		}
		UmsShopInfo updateInfo = new UmsShopInfo();
		updateInfo.setId(shopInfoId);
		updateInfo.setStatus(status);
		updateInfo.setReason(reason);
		updateInfo.setUpdTime(DateUtil.date());
		shopInfoMapper.updateByPrimaryKeySelective(updateInfo);

		if(status == ShopEnums.ShopStatus.SUCCESS.getValue()){


			UmsMember updateMember = new UmsMember();
			updateMember.setId(shopInfo.getMemberId());
			updateMember.setType(CommonConstant.SELLER);



			if(shopInfo.getShoptype().equals(2)){
				updateMember.setIsTansport(true);
			}

			if(shopInfo.getShoptype().equals(3)){
				updateMember.setIsWelfare(true);
			}

			if(shopInfo.getShoptype().equals(4)){
				updateMember.setIsRescue(true);
			}
			if(shopInfo.getShoptype().equals(5)){
				updateMember.setIsHospital(true);
			}

			if(shopInfo.getShoptype().equals(6)){
				updateMember.setIsFarm(true);
			}




			//稍微调整下，只有店铺才给创建店铺号
			if(shopInfo.getShoptype().equals(1)){
				UmsShop shop = new UmsShop();

				updateMember.setIsMer(true);

				//审核通过，创建店铺
				BeanUtil.copyProperties(shopInfo, shop);
				shop.setKeyword(shop.getName());
				shop.setLevel(0);
				shop.setSort(9999);
				shop.setStatus(CommonConstant.SUS_INT);
				shop.setAddTime(DateUtil.date());
				shop.setUpdTime(shop.getAddTime());

				shop.setUpdTime(shop.getAddTime());

				try {
					String result = amapUtil.getLonLat(shop.getAddress());
					if(result!=null){
						String[] lnglat = result.split(",");
						shop.setLongitude(lnglat[0]);
						shop.setLatitude(lnglat[1]);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}



				 String address = shop.getAddress();
				 if(address!=null && address.length()>10 && address.contains(",")){

					 String result = amapUtil.getLonLat(address);
					 String[] longlat_arr= result.split(",");
					 if(longlat_arr!=null  && longlat_arr[0].length()>0 && longlat_arr[1].length()>0){
						 shop.setLongitude(longlat_arr[0]);
						 shop.setLatitude(longlat_arr[1]);
					 }
				 }

				shopMapper.insertSelective(shop);


				updateMember.setShopId(shop.getId());


				updateMember.setNickname(shop.getName());
				updateMember.setHeadImg(shop.getLogo());






//			ChatNotice notice = new ChatNotice();
//			notice.setTitle("「"+shopInfo.getName()+"」申请新开店铺, 请尽快审批");
//			notice.setContent(JSONUtil.toJsonStr(shopInfo));
//			notice.setType(MemberRemindEnums.AdminRemindTypes.WAIT_VERIFY_SHOP.getType());
//			chatService.sendNoticeMsg(CustomerConstant.ADMIN_MEMBER_ID, JSONUtil.toJsonStr(notice));


				UmsShopAccountExample sa_example = new UmsShopAccountExample();
				sa_example.createCriteria().andMemberIdEqualTo(shopInfo.getMemberId());
				PageHelper.startPage(1, 1);

				List<UmsShopAccount> list = shopAccountMapper.selectByExample(sa_example);
				System.out.println("listlistlistlistlistlist");
				System.out.println("listlistlistlistlistlist");
				System.out.println(list.toString());


				if(list==null  ||  list.size()<=0) {

					System.out.println("listlistlistlistlistlist111111111111111111");
					UmsMember member = memberService.getMemberById(shopInfo.getMemberId());


					if(member.getPhone().equals("") || member.getPhone()==null){
						throw new ServiceException("用户手机号不能为空");
					}


					System.out.println("passwordpasswordpassword1");
					String oriPassword = member.getPassword();
					System.out.println("passwordpasswordpassword2");
					//注册子账号
					UmsShopAccount account = new UmsShopAccount();
					System.out.println("passwordpasswordpassword3");
					account.setMemberId(shopInfo.getMemberId());
					System.out.println("passwordpasswordpassword4");
					account.setUsername(!member.getPhone().equals("")?member.getPhone():member.getCode());
//					account.setPayPassword(passwordEncoder.encode());
					System.out.println("passwordpasswordpassword5");
					String password =com.chengyu.core.utils.StringUtils.getStringRandom(10);
					System.out.println("password1");
					System.out.println(password);
					account.setPassword( CommonUtil.createPwd(password));
//					account.setPassword(oriPassword);
					account.setPhone(member.getPhone());
					account.setMainInd(CommonConstant.YES_INT);
					account.setRealname(shopInfo.getName());
					account.setShopId(shop.getId());
					account.setShopName(shop.getName());
//					account.setShopName(shop.getName());
					shopAccountService.register(account);

					Map<String,String> templateParam = new HashMap<>(16);
					verifyCodeService.sendSms(member.getPhone(), "7", templateParam);
//					return CommonResult.success("发送成功");

				}else{

					System.out.println("listlistlistlistlistlist2222");
					//			//更新子账号
					UmsShopAccountExample example = new UmsShopAccountExample();
					example.createCriteria().andMemberIdEqualTo(shopInfo.getMemberId()).andShopIdIsNull();
					UmsShopAccount updateAccount = new UmsShopAccount();
					updateAccount.setShopId(shop.getId());
					updateAccount.setShopName(shop.getName());
					shopAccountMapper.updateByExampleSelective(updateAccount, example);
				}


//			//更新子账号
//			UmsShopAccountExample example = new UmsShopAccountExample();
//			example.createCriteria().andMemberIdEqualTo(shopInfo.getMemberId()).andShopIdIsNull();
//			UmsShopAccount updateAccount = new UmsShopAccount();
//			updateAccount.setShopId(shop.getId());
//			updateAccount.setShopName(shop.getName());
//			shopAccountMapper.updateByExampleSelective(updateAccount, example);

				WalkMember walkMember = walkMemberService.getWalkMemberByMember(shopInfo.getMemberId());
				if(walkMember != null){
					WalkMember updateWalk = new WalkMember();
					updateWalk.setId(walkMember.getId());
					updateWalk.setType(CommonConstant.SELLER);
					updateWalk.setNickname(shop.getName());
					updateWalk.setHeadImg(shop.getLogo());
					walkMemberService.updateWalkMember(updateWalk);
				}

				shopConfigService.initShopConfig(shop);

			}

			memberService.updateMember(updateMember);



		}
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteShopInfo(Integer shopInfoId) throws ServiceException {
		UmsShopInfo shopInfo = shopInfoMapper.selectByPrimaryKey(shopInfoId);
		if(shopInfo == null || shopInfo.getStatus() == ShopEnums.ShopStatus.SUCCESS.getValue()){
			throw new ServiceException("入驻成功的店铺无法删除");
		}
		shopInfoMapper.deleteByPrimaryKey(shopInfoId);
	}
}
